<!--
 * @Author: chenhaoran
 * @Date: 2024-08-08 10:56:21
 * @LastEditors: chenhaoran
 * @LastEditTime: 2024-08-08 10:57:46
-->
<template>
  <div class="statistic">
    <el-row :gutter="20" class="card-wrap">
      <el-col :span="6">
        <el-card shadow="hover" class="card-body">
          <el-icon color="#fff" size="50" class="card-icon color__blue">
            <User />
          </el-icon>
          <div class="card-content">
            <el-statistic title="用户访问量" :value="userViewVal" :value-style="{color: color__blue}" />
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card shadow="hover" class="card-body">
          <el-icon color="#fff" size="50" class="card-icon color__green">
            <ChatDotRound />
          </el-icon>
          <div class="card-content">
            <el-statistic title="系统消息" :value="sysMessageVal" :value-style="{color: color__green}" />
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card shadow="hover" class="card-body">
          <el-icon color="#fff" size="50" class="card-icon color__red">
            <Goods />
          </el-icon>
          <div class="card-content">
            <el-statistic title="商品数量" :value="goodsVal" :value-style="{color: color__red}" />
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card shadow="hover" class="card-body">
          <el-icon color="#fff" size="50" class="card-icon color__orange">
            <ShoppingCartFull />
          </el-icon>
          <div class="card-content">
            <el-statistic title="今日订单量" :value="orderVal" :value-style="{color: color__orange}" />
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { ref, reactive, toRef, toRefs, computed } from 'vue'
import { useTransition } from '@vueuse/core'
import api from '@/api/home.js'
defineOptions({
  name: 'statistic'
})
const color__blue = ref('#2d8cf0')
const color__green = ref('#64d572')
const color__red = ref('#f25e43')
const color__orange = ref('#e9a745')

const source = ref({
  userView: 0,
  sysMessage: 0,
  goods: 0,
  order: 0
})
// 如果使用reactive最好嵌套一层，这样可以给整个对象赋值
// const source = reactive({
//   data: {
//     userView: 0,
//     sysMessage: 0,
//     goods: 0,
//     order: 0
//   }
// })

const getTransition = (value) => {
  const output = useTransition(value || 0, {
    duration: 1500
  })
  return output
}

const {userView, sysMessage, goods, order} = toRefs(source.value)
const userViewVal = getTransition(userView)
const sysMessageVal = getTransition(sysMessage)
const goodsVal = getTransition(goods)
const orderVal = getTransition(order)

const getStatisticData = () => {
  api.getStatisticData().then(res => {
    source.value = Object.assign(source.value, res.data)
  })
}
getStatisticData()
</script>
<style lang="scss">
.statistic {
  .card-body .el-card__body {
    display: flex;
    height: 100px;
    align-items: center;
    padding: 0;
    .card-icon {
      width: 100px;
      height: 100px;
    }
    .card-content {
      flex: 1;
      display: flex;
      justify-content: center;
      align-items: center;
    }
  }
  .color__blue {
    background: v-bind(color__blue);
  }
  .color__green {
    background: v-bind(color__green);
  }
  .color__red {
    background: v-bind(color__red);
  }
  .color__orange {
    background: v-bind(color__orange);
  }
}
</style>
<style lang="scss" scoped>
:deep(.el-statistic) {
  display: flex;
  flex-direction: column-reverse;
  justify-content: center;
  align-items: center;
}
:deep(.el-statistic__head) {
  color: #a699b3;
  font-size: 14px;
}
:deep(.el-statistic__content) {
  font-size: 28px;
}
</style>